 package com.suozq.current.reentrantlock;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 锁战强
 * @date 2022/04/26
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore  s = new Semaphore(2);
        ThreadPoolExecutor pool = new ThreadPoolExecutor(4, 8, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(50));
        pool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        for(int i=0;i<10;i++) {
            final int num = i;
            pool.execute(()->{
                try {
                    s.acquire();
                    System.out.println("num:"+num+"开始检票----");
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                     e.printStackTrace();
                     Thread.currentThread().interrupt();
                }finally {
                    s.release();
                    System.out.println("----num:"+num+"检票结束");
                }
            });
        }
        pool.shutdown();
    }
}
